package com.xg.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xg.sys.entity.SysOrganizationEntity;
import com.xg.sys.service.SysOrganizationService;
import com.xg.sys.service.SysUserOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xg.sys.mapper.SysUserOrgMapper;
import com.xg.sys.entity.SysUserOrgEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class SysUserOrgServiceImpl extends ServiceImpl<SysUserOrgMapper, SysUserOrgEntity> implements SysUserOrgService {
    @Autowired
    SysOrganizationService organizationService;

    @Override
    public void pageList(Page<SysUserOrgEntity> page, SysUserOrgEntity data) {

        QueryWrapper<SysUserOrgEntity> queryWrapper = new QueryWrapper<SysUserOrgEntity>();
        baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<Long> getOidListByUid(Long id) {
        List<SysUserOrgEntity> list = this.list(
                new QueryWrapper<SysUserOrgEntity>().lambda()
                        .select(SysUserOrgEntity::getOid)
                        .eq(SysUserOrgEntity::getUserId, id)
        );
        List<Long> oidList = list.stream().map(item -> item.getOid()).collect(Collectors.toList());
        return oidList;
    }

    @Override
    public List<Long> getUserIdListByOid(Long oid) {
        List<Long> oidList = new ArrayList<>();
        List<SysOrganizationEntity> allList = organizationService.list(null);
        setOidList(allList,oidList,oid);

        List<SysUserOrgEntity> list = this.list(
                new QueryWrapper<SysUserOrgEntity>().lambda()
                        .select(SysUserOrgEntity::getUserId
)
                        .in(SysUserOrgEntity::getOid,oidList)
        );
        return list.stream().map((item -> item.getUserId
())).collect(Collectors.toList());
    }

    @Override
    public boolean removeByUserId(Long id) {
        return super.remove(
                new LambdaQueryWrapper<SysUserOrgEntity>()
                        .eq(SysUserOrgEntity::getUserId, id)
        );
    }

    private void setOidList(List<SysOrganizationEntity> allList,List<Long> oidList,Long oid) {
        List<SysOrganizationEntity> childList = allList.stream().filter(item -> oid.equals(item.getParentId())).collect(Collectors.toList());
        if (childList.isEmpty()) {
            oidList.add(oid);
        } else {
            for (SysOrganizationEntity child : childList) {
                setOidList(allList,oidList,child.getId());
            }
        }
    }

    @Override
    public void relateUser(SysUserOrgEntity userOrgEntity) {
        removeByUserId(userOrgEntity.getUserId
());
        this.save(userOrgEntity);
    }

    @Override
    public List<SysUserOrgEntity> getOidListByUidList(List<Long> uidList) {
        return this.list(
                new LambdaQueryWrapper<SysUserOrgEntity>()
                        .in(SysUserOrgEntity::getUserId, uidList)
        );
    }
}
